<template>
    <div>
        <!--所有栏目-->
        <div class="category" @click="showPopup">
            <van-icon name="label-o" size="40" color="gray"/>
        </div>
        <van-popup v-model="show" position="bottom" class="category-content">
            <van-icon name="cross" size="30" @click="cross" />
            <!--我的频道-->
            <div class="my-channel">
                <div class="title">
                    <div class="text">我的频道<span>点击进入频道</span></div>
                    <div class="redact" v-show="!isEdit" @click="redact">编辑</div>
                    <div class="redact" v-show="isEdit" @click="complete">完成</div>
                </div>
                <div class="add-content">
                    <ul>
                        <li>推荐</li>
                        <li v-for="(item,index) in subscriptionCategoryList" :key="index" @click="cancel(index,item.name)">
                            {{item.name}}
                        </li>
                    </ul>
                </div>
            </div>
            <!--推荐频道-->
            <div class="recommend-channel">
                <div class="title">
                    <div class="text">频道推荐<span>点击添加频道</span></div>
                </div>
                <div class="channel-content">
                    <ul>
                        <li v-for="(item,index) in unsubscriptionCategoryList" :key="index" @click="pick(item)">
                            <div class="plus">
                                <van-icon name="plus" size="20" />
                            </div>
                            <div class="category-title">
                                {{item.name}}
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script>
    import {getClassify, subscription} from "../api/CMS-api"
    import {mapState} from "vuex";
    import {
        CHANGE_SUBSCRIPT_CATEGORY,
        ADD_SUBSCRIPT_CATEGORY
    } from "../store/mutaions_type";

    export default {
        created() {
            getClassify(1, 22).then(res => {
                this.classifyList = res.rows
            });
        },
        data() {
            return {
                show: false,
                classifyList: [],
                addList: [],
                isEdit: false,
            }
        },
        methods: {
            showPopup() {
                this.show = true;
            },
            cross() {
                this.show = false
            },
            pick(category) {
                if (!this.isEdit) {
                    return
                }
                this.$store.commit(ADD_SUBSCRIPT_CATEGORY, {
                    category
                })
            },
            redact() {
                this.isEdit = true;
            },
            complete() {
                const categoryIdList = this.subscriptionCategoryList.map(item => item.categoryId);
                const categoryIdsStr = categoryIdList.join(",");
                subscription(categoryIdsStr).then(res => {
                    if (res.code === 0) {
                        this.$toast("操作成功")
                    }
                    this.isEdit = false
                });
            },
            cancel(index) {
                if (!this.isEdit) {
                    return
                }
                // 取消订阅
                const categoryList = JSON.parse(JSON.stringify(this.subscriptionCategoryList));
                categoryList.splice(index, 1);
                this.$store.commit(CHANGE_SUBSCRIPT_CATEGORY, {
                    subscriptionCategoryList: categoryList
                })
            }
        },
        computed: {
            ...mapState({
                subscriptionCategoryList: "subscriptionCategoryList"
            }),
            unsubscriptionCategoryList() {
                return this.classifyList.filter(item => {
                    return !this.subscriptionCategoryList.some(subCategoryItem => {
                        return subCategoryItem.categoryId === item.categoryId;
                    })
                })
            }
        }
    }
</script>

<style lang="less" scoped>
    .category {
        width: 40px;
        height: 44px;
        background-color: #ffffff;
        position: absolute;
        top: 54px;
        right: 0;
        z-index: 101;
    }
    .category-content {
        width: 100%;
        height: 750px;
        border-radius: 7px 7px 0 0;
        .my-channel {
            width: 100%;
            height: 350px;
            .title {
                width: 100%;
                height: 30px;
                .text {
                    font-size: 17px;
                    font-family: "\5B8B\4F53";
                    padding-left: 5px;
                    float: left;
                    height: 30px;
                    line-height: 30px;
                    span {
                        font-size: 15px;
                        color: darkgray;
                        padding-left: 10px;
                    }
                }
                .redact {
                    width: 58px;
                    height: 28px;
                    border-radius: 15px;
                    border: 1px solid #f34f4f;
                    float: right;
                    color: #f34f4f;
                    font-size: 17px;
                    font-family: "\5B8B\4F53";
                    line-height: 30px;
                    text-align: center;
                }
            }
            .add-content {
                width: 100%;
                height: 300px;
                ul {
                    li {
                        width: 70px;
                        height: 33px;
                        margin: 10px 10px;
                        display: inline-block;
                        border: 1px solid black;
                        border-radius: 5px;
                        line-height: 35px;
                        text-align: center;
                    }
                }
            }
        }
        .recommend-channel {
            width: 100%;
            height: 400px;
            .title {
                width: 100%;
                height: 30px;
                .text {
                    font-size: 17px;
                    font-family: "\5B8B\4F53";
                    padding-left: 5px;
                    float: left;
                    height: 30px;
                    line-height: 30px;
                    span {
                        font-size: 15px;
                        color: darkgray;
                        padding-left: 10px;
                    }
                }
            }
            .channel-content {
                width: 100%;
                height: 400px;
                ul {
                    li {
                        width: 70px;
                        height: 33px;
                        margin: 10px 10px;
                        display: inline-block;
                        border: 1px solid black;
                        border-radius: 5px;
                        line-height: 35px;
                        .plus {
                            float: left;
                            margin: 2px 3px 0 4px;
                        }
                        .category-title {
                            float: left;
                        }
                    }
                }
            }
        }
    }
</style>
